package m.jclecai.com.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import m.jclecai.com.R;
import m.jclecai.com.bean.OpenListBean;

/**
 * Created by hp on 2017/3/15.
 */

public class LotteryHistoryAdapter extends BaseAdapter {

    private Context context;
    private List<OpenListBean.Records> bean;

    public LotteryHistoryAdapter(Context context, List<OpenListBean.Records> bean){
        this.bean = bean;
        this.context = context;
    }

    @Override
    public int getCount() {
        return bean.size();
    }

    @Override
    public Object getItem(int position) {
        return bean.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.lottery_history_item,null);
            holder.tv_ball_one = (TextView) convertView.findViewById(R.id.tv_ball_one);
            holder.tv_ball_two = (TextView) convertView.findViewById(R.id.tv_ball_two);
            holder.tv_ball_three = (TextView) convertView.findViewById(R.id.tv_ball_three);
            holder.tv_ball_four = (TextView) convertView.findViewById(R.id.tv_ball_four);
            holder.tv_ball_five = (TextView) convertView.findViewById(R.id.tv_ball_five);
            holder.tv_ball_six = (TextView) convertView.findViewById(R.id.tv_ball_six);
            holder.tv_ball_seven = (TextView) convertView.findViewById(R.id.tv_ball_seven);
            holder.tv_ball_eight = (TextView) convertView.findViewById(R.id.tv_ball_eight);
            holder.tv_ball_nine = (TextView) convertView.findViewById(R.id.tv_ball_nine);
            holder.tv_ball_ten = (TextView) convertView.findViewById(R.id.tv_ball_ten);
            holder.tv_ball_eleven = (TextView) convertView.findViewById(R.id.tv_ball_eleven);
            holder.tv_ball_twelve = (TextView) convertView.findViewById(R.id.tv_ball_twelve);
            holder.tv_ball_thirteen = (TextView) convertView.findViewById(R.id.tv_ball_thirteen);
            holder.tv_ball_fourteen = (TextView) convertView.findViewById(R.id.tv_ball_fourteen);
            holder.tv_date = (TextView) convertView.findViewById(R.id.tv_date);
            convertView.setTag(holder);
        }else{
           holder = (ViewHolder) convertView.getTag();
        }
        String term = bean.get(position).getTerm();
        String time = bean.get(position).getTime();
        String type = bean.get(position).getType();
        holder.tv_date.setText("第"+term+"期"+"　"+time);

        holder.tv_ball_one.setTag(time);
        holder.tv_ball_two.setTag(term);
        holder.tv_ball_three.setTag(type);


        TextView[] texts = new TextView[]{holder.tv_ball_one,holder.tv_ball_two,holder.tv_ball_three,holder.tv_ball_four,holder.tv_ball_five,
                holder.tv_ball_six, holder.tv_ball_seven,holder.tv_ball_eight,holder.tv_ball_nine,holder.tv_ball_ten,holder.tv_ball_eleven,
                holder.tv_ball_twelve, holder.tv_ball_thirteen, holder.tv_ball_fourteen };

        //切割|后的数据
        String[] result = null;
        //红球数据
        String[] redBall = null;
        //蓝球数据
        String[] blueBall = null;
        //总数据长度
        int length;

        String codes = bean.get(position).getResult();
        if(codes.contains("|")){
            //红球加蓝球
            result = codes.split("\\|");
            redBall = result[0].split(",");
            blueBall = result[1].split(",");
        }else{
            //只有红球
            redBall = codes.split(",");
        }
        if(blueBall != null){
            length = redBall.length + blueBall.length;
        }else{
            length = redBall.length;
        }
        setLotteryHistoryView(texts,redBall,blueBall,length);
        return convertView;
    }

    private void setLotteryHistoryView(TextView[]texts,String[] redBall,String[]blueBall,int length){
        //根据实际长度显示球数
            for(int i = 0; i < texts.length; i++){
                if(i < length){
                    texts[i].setVisibility(View.VISIBLE);
                }else{
                    texts[i].setVisibility(View.GONE);
                }
            }
        //设置红球数据
            for(int i = 0;i < redBall.length;i++){
                texts[i].setText(redBall[i]);
                texts[i].setTextColor(context.getResources().getColor(R.color.baseRed));
            }
         //设置蓝球数据
            if(blueBall != null){
                for(int i = 0; i < blueBall.length; i++){
                    texts[redBall.length + i].setText(blueBall[i]);
                    texts[redBall.length + i].setTextColor(context.getResources().getColor(R.color.baseBlue));
            }
        }
    }


    public static class ViewHolder{
        TextView tv_date;
        public TextView tv_ball_one;
        public TextView tv_ball_two;
        public TextView tv_ball_three;
        TextView tv_ball_four;
        TextView tv_ball_five;
        TextView tv_ball_six;
        TextView tv_ball_seven;
        TextView tv_ball_eight;
        TextView tv_ball_nine;
        TextView tv_ball_ten;
        TextView tv_ball_eleven;
        TextView tv_ball_twelve;
        TextView tv_ball_thirteen;
        TextView tv_ball_fourteen;
    }
}
